/**
 * Project Name:SystemUtil.java
 * File Name:SystemUtil.java
 * Date:2018/9/4 下午2:25
 * Copyright (c) 2018, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.dyrj.base.system.utils;

import com.dyrj.base.system.menu.model.SysMenu;
import com.dyrj.base.system.org.model.TDepartment;
import com.dyrj.base.system.user.dao.SysUserMapper;
import com.dyrj.component.SpringUtil;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.utils.MD5Util;
import com.dyrj.utils.UUIDUtil;

import java.util.Iterator;
import java.util.List;

/**
 * @Title: SystemUtil.java
 * @Description: todo(系统工具类)
 * @Author:
 * @Date: 2018/9/4 下午2:25
 * @Version: V1.0
 */
public class SystemUtil {

    /**
     * @Title: deleteNotPermisMenu
     * @Description: todo(递归清除空children)
     * @Params: [menus：菜单集合, paermis：权限字符串]
     * @Return: java.util.List<com.dyrj.base.system.menu.model.SysMenu>    返回类型
     * @Author:
     * @Date: 2018/9/4 下午2:29
     */
    public static List<SysMenu> deleteNotPermisMenu(List<SysMenu> menus, String paermis) {
        Iterator<SysMenu> iterator = menus.iterator();
        while(iterator.hasNext()) {
            SysMenu menu = iterator.next();
            if(!paermis.contains(menu.getPerms())) {
                iterator.remove();
            } else {
                if(!menu.getChildren().isEmpty()) {
                    deleteNotPermisMenu(menu.getChildren(), paermis);
                }
            }
        }
        return menus;
    }

    /**
     * @Title: deleteNullChildren
     * @Description: todo(递归清除空children)
     * @Params: [menus：菜单集合]
     * @Return: java.util.List<com.dyrj.base.system.menu.model.SysMenu>    返回类型
     * @Author:
     * @Date: 2018/9/4 下午2:29
     */
    public static List<SysMenu> deleteNullChildren(List<SysMenu> menus) {
        for(SysMenu info : menus) {
            if(info.getChildren().isEmpty()) {
                info.setChildren(null);
            } else {
                deleteNullChildren(info.getChildren());
            }
        }
        return menus;
    }

    /**
     * @Title: createAppid
     * @Description: todo(生成不重复的appid)
     * @Params: [salt]
     * @Return: java.lang.String    返回类型
     * @Author:
     * @Date: 2018/8/28 下午6:20
     */
    public static String createAppid(String salt) {
        SysUserMapper mapper = SpringUtil.getBean(SysUserMapper.class);
        // 设置appid
        String appid = SysFrameConst.APPID_PREFIX + MD5Util.md5_16(UUIDUtil.uuid(), salt);
        int count = mapper.selectCountByAppid(appid);
        if(count > SysFrameConst.ZERO) {
            createAppid(salt);
        } else {
            return appid;
        }
        return appid;
    }
    public static List<TDepartment> deleteDepNullChildren(List<TDepartment> menus) {
        for(TDepartment info : menus) {
            if(info.getChildren().isEmpty()) {
                info.setChildren(null);
            } else {
                deleteDepNullChildren(info.getChildren());
            }
        }
        return menus;
    }
}
